define(function (require, exports, module) {
    'use strict';
    var $ = require('jquery'),
        _ = require('underscore'),
        Backbone = require('backbone'),
        notify = require('js/utils/notify'),
        models = require('js/apps/op/models'),
        utils = require('js/utils/tools'),
        tz = require('js/utils/tz'),
        moment = require('moment'),
        app = Backbone.history;
    require('datetimepicker');
    var MetisTigerPartListView = Backbone.View.extend({
        tagName: "div",
        events: {
            'click #search': 'doSearch',
        },
        initialize: function (options) {
            this.options = options;
            this.collection = new models.MetisTigerPartCollection();
            this.collection.bind('change reset remove', this.renderWithData, this);
        },
        render: function () {
            this.$el.html("");
            $(window).scrollTop(0);
            return this;
        },
        doSearch: function () {
            var options = {},
                searched = {};
            searched.round_id = parseInt($('#searchRoundId').val());
            searched.uid = parseInt($('#searchUid').val());
            searched.bet_index = parseInt($('#searchBetIndex').val());
            searched.create_time_lower = $("#searchCreatedAtLower").val();
            searched.create_time_upper = $('#searchCreatedAtUpper').val();
            if (!isNaN(searched.uid)) {
                options.user_id = searched.uid;
            }
            if (!isNaN(searched.term)) {
                options.round_id = searched.round_id;
            }
            if (searched.bet_index != -1) {
                options.bet_index = searched.bet_index;
            }
            if (searched.create_time_lower || searched.create_time_upper) {
                options.create_time = {};
                if (searched.create_time_lower) options.create_time.$gte = searched.create_time_lower;
                if (searched.create_time_upper) options.create_time.$lt = searched.create_time_upper;
            }
            utils.saveSearched('op/metis_tiger_part', searched);
            app.navigate(utils.composeQueryString('#op/metis_tiger_part/', options), {
                trigger: true
            });
        },
        renderWithData: function () {
            this.$el.html(this.template({
                items: this.collection.toJSON(),
                total_bet: this.collection.total_bet,
                total_win: this.collection.total_win,
                searched: utils.getSearched('op/metis_tiger_part')
            }));
            var that = this;
            $('.refreshStatus').click(function() {
                var data_id = $(this).data('id');
                $.post('/admin/metis_tiger_part/'+data_id+'/fresh/').done(function (resp) {
                  var data = resp.data;
                  notify.success('提示', '刷新成功！');
                  that.load();
                }).fail(function (data) {
                  notify.notifyResp(data);
                });
            });
            utils.renderTable('main-list', {
                $orderby: that.options.$orderby || 'id',
                sortCallback: function (field) {
                    that.options.$orderby = field;
                    that.options.$page = 1;
                    var newUrl = utils.composeQueryString('#op/metis_tiger_part/', that.options);
                    app.navigate(newUrl, {
                        trigger: true
                    });
                }
            });
            $('[data-type=date]').datetimepicker({
                defaultDate: false,
                format: 'YYYY-MM-DD HH:mm:ss'
            });
            utils.getPaginator(that.options, this.collection.total, '#op/metis_tiger_part/');
  
            return this;
        },
        load: function () {
            this.options.game_id = 5503
            this.collection.fetch({
                reset: true,
                data: this.options,
                error: function (c, r, o) {
                    notify.notifyResp(r);
                },
            });
        }
    });
    exports.List = Backbone.View.extend({
        el: "#content_wrapper",
        template: "op/MetisTigerPartList.html",
  
        initialize: function (options) {
            this.options = options;
        },
        render: function () {
            var view = new MetisTigerPartListView(this.options);
            view.template = this.template;
  
            this.$el.empty();
            this.$el.append(view.render().el);
            view.load();
  
            return this;
        }
    });
  
  });
  